package com.china08.yxyapi.api.manage.tec.teaching.lesson.school;

import java.sql.Timestamp;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsSchoolLesson;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsSchoolLessonItem;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsVideo;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.YxyLessonsSchoolLessonItemRepository;
import com.china08.yxyapi.repository.db.YxyLessonsSchoolLessonRepository;
import com.china08.yxyapi.repository.db.YxyLessonsVideoRepository;
import com.china08.yxyapi.util.StringTrimUtil;

import io.swagger.annotations.ApiOperation;

@CrossOrigin
@RestController
public class YxyLessonsSchoolUpdItemController extends YxyApiBaseController {

    @Autowired
    YxyLessonsSchoolLessonItemRepository itemRepository;

    @Autowired
    YxyLessonsSchoolLessonRepository lessonRepository;

    @Autowired
    YxyLessonsVideoRepository videoRepository;

    @ApiOperation("")
    @RequestMapping(value = "/api_yw/manage/tec/teaching/school/item/upd", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel process(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {
        String itemId = reqModel.itemId;
        String title = StringTrimUtil.trim(reqModel.title);
        String intro = reqModel.intro;
        String videoId = reqModel.videoId;
        YxyLessonsSchoolLessonItem item = itemRepository.findById(itemId);
        if (null != item) {

            // 修改课时时常
            Integer oldDuration = 0;
            Integer newDuration = 0;
            YxyLessonsVideo video = videoRepository.findById(item.getVideoId());
            if (null != video) {
                oldDuration = video.getDuration();
            }
            video = videoRepository.findById(videoId);
            if (null != video) {
                newDuration = video.getDuration();
            }

            item.setTitle(title);
            item.setIntro(intro);
            item.setVideoId(videoId);
            item.setLastModifiedDate(new Timestamp(new Date().getTime()));
            item.setStatus(0);
            itemRepository.save(item);

            // 课程变成已下架
            String lessonId = item.getLessonId();
            YxyLessonsSchoolLesson lesson = lessonRepository.findById(lessonId);
            if (null != lesson) {
                lesson.setIsVisible(0);
                lesson.setDownType("其它");
                lesson.setDuration(lesson.getDuration() - oldDuration + newDuration);
                lessonRepository.save(lesson);
            }
        }
        return null;
    }

    public static class ReqModel extends YxyApiReqModel {
        public String itemId;
        public String title;
        public String intro;
        public String videoId;

        @Override
        public void autoCheck() {
            Assert.isTrue(StringUtils.isNotBlank(itemId), "课程不能为空");
            Assert.isTrue(StringUtils.isNotBlank(title), "课时名称不能为空");
            Assert.isTrue(StringUtils.isNotBlank(videoId), "视频不能为空");
            Assert.isTrue(title.length() > 0 && title.length() < 36, "请输入1-35个字的视频名称");
            if (StringUtils.isNotBlank(intro)) {
                Assert.isTrue(intro.length() > 0 && intro.length() < 501, "请输入1-500个字的课时简介");
            }
        }
    }

    public static class RespModel extends YxyApiRespModel {
        public Integer itemLength;
    }
}
